/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.gen.seam.generator.template;

/**
 *
 * @author Gustavo
 */
public class BeanTemplate extends Template {

    private StringBuffer bean = new StringBuffer("");
    private String entityName, entityVariable, beanVariable, listBeanVariable, daoName, daoVariable;

    private void instatiateVariables(){
        entityName = getTable().getName();
        entityVariable = Character.toLowerCase(entityName.charAt(0)) + entityName.substring(1);
        daoName = entityName + "Dao";
        daoVariable = Character.toLowerCase(daoName.charAt(0)) + daoName.substring(1);
        beanVariable = Character.toLowerCase(entityName.charAt(0)) + entityName.substring(1);
        listBeanVariable = beanVariable + "List";
    }

    @Override
    public void generate() {
        instatiateVariables();
        init();
        body();
        saveUpdateMethod();
        changeStateMethod();
        findByNameMethod();
        findAllMethod();
        generateGetAndSet();
    }


    private void init(){
        bean.append("import java.io.Serializable;\n")
            .append("import java.util.ArrayList;\n")
            .append("import java.util.List;\n")

            .append("import org.jboss.seam.ScopeType;\n")
            .append("import org.jboss.seam.annotations.In;\n")
            .append("import org.jboss.seam.annotations.Name;\n")
            .append("import org.jboss.seam.annotations.Scope;\n")
            .append("import org.jboss.seam.annotations.datamodel.DataModel;\n")
            .append("import br.com.pdcase.base.core.AbstractWebBean;\n")
            .append("import br.com.pdcase.base.exception.DaoException;\n")

            .append("\n")
            .append("@Name(\"").append(entityName).append("Bean\")\n")
            .append("@Scope(ScopeType.CONVERSATION)\n")
            .append("public class ").append(entityName).append("Bean extends AbstractWebBean implements Serializable {\n")
            .append("private static final long serialVersionUID = 1;\n\n");
    }

    private void body(){
        bean.append("@In(value = \"#{").append(daoName).append("}\")\n")
            .append("private ").append(daoName).append(" ").append(daoVariable).append(";\n\n")
            
            .append("private " + entityName + " " + entityVariable + " = new " + entityName +"();\n\n")

            .append("@DataModel\n")
            .append("private List<" + entityName + "> " + listBeanVariable + " = new ArrayList<" + entityName + ">();\n\n");
    }

    private void saveUpdateMethod(){
        bean.append("public void confirmar() {\n")
            .append("try {\n")
            .append("if("+entityVariable+".getId().intValue() == 0) {\n")
            .append(daoVariable + ".save("+entityVariable+");\n")
            .append("} else {\n")
            .append(daoVariable + ".merge("+entityVariable+");\n")
            .append("}\n")
            .append("} catch (Exception e) {\n")
            .append("e.printStackTrace();\n")
            .append("}\n")
            .append("}\n");
    }

    private void changeStateMethod(){
        bean.append("public void mudarStatus(" + entityName + " " + entityVariable+") {\n")
            .append("if ("+ entityVariable + ".getStatus().equals(\"I\")) {\n")
            .append(entityVariable + ".setStatus(\"A\");\n")
            .append("} else {\n")
            .append(entityVariable + ".setStatus(\"I\");\n}\n")
            
            .append("try {\n")
            .append(daoVariable + ".merge("+entityVariable +");\n")
            .append("listar();\n")
            .append("} catch (Exception e) {\n")
            .append("e.printStackTrace();\n")
            .append("}\n}\n");
    }
    
    private void findByNameMethod(){
    }

    private void findAllMethod(){
        bean.append("public void listar() {\n")
            .append(entityVariable + " = "+ daoVariable+".recuperarTodos();\n")
            .append("}\n");
    }

    private void generateGetAndSet(){
        bean.append("public "+ entityName + " get" +entityName +"(){\n")
            .append("return "+entityVariable + ";\n")
            .append("}\n")

            .append("public void set"+entityName+"("+entityName + " " + entityVariable + ") {\n")
            .append("this."+entityVariable + " = " + entityVariable + ";\n")
            .append("}\n")

            .append("public List<"+entityName+"> get"+entityName+"List() {\n")
            .append("return "+entityVariable+"List;\n")
            .append("}\n");
    }

    @Override
    public String toString(){
        return bean.toString();
    }
}
